//
//  XZHHomeDetailViewController.m
//  Piece
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHHomeDetailViewController.h"
#import "XZHHomeTableView.h"
#import "Masonry.h"
#import "XZHHomeNoteViewController.h"
#import "XZHSearchViewController.h"
#import "JZShareCustom.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@interface XZHHomeDetailViewController ()<HomeTableViewCellDelegate>
@property (nonatomic, strong) UILabel *likeLabel;
@end

@implementation XZHHomeDetailViewController {
    BOOL isLike;
    NSInteger presentLikeCount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(150, -10, 55, 30);
    label.text = @"PIECE";
    label.font = [UIFont fontWithName:@"Helvetica" size:20];
    label.textColor = [UIColor colorWithRed:0.29 green:0.36 blue:0.42 alpha:1.00];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    
    [self setUpTags];
    [self setUpHomeDetailTableView];
}

- (void)setUpTags {
    UIButton *btn_note = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_note setBackgroundImage:[UIImage imageNamed:@"diary_normal"] forState:UIControlStateNormal];
    [self.view addSubview:btn_note];
    [btn_note makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(10);
        make.bottom.equalTo(self.view.bottom).offset(-30);
        make.height.equalTo(20);
        make.width.equalTo(50);
    }];
    
    UIButton *btn_more = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_more setBackgroundImage:[UIImage imageNamed:@"share_image"] forState:UIControlStateNormal];
    [self.view addSubview:btn_more];
    [btn_more makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.right).offset(-10);
        make.top.equalTo(btn_note).offset(-14);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
    
    UIButton *btn_heart = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_heart];
    [btn_heart makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btn_more.left).offset(-40);
        make.top.equalTo(btn_more).offset(1);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
    [btn_heart setTag:1200];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"like_%@",self.model.hpcontent_id]]) {
        [btn_heart setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateNormal];
        presentLikeCount += 1;
        self.likeLabel.text = [NSString stringWithFormat:@"%ld", presentLikeCount];
        isLike = YES;
    } else {
        [btn_heart setBackgroundImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
    }
    
    self.likeLabel = [[UILabel alloc] init];
    self.likeLabel.textColor = [UIColor colorWithRed:0.74 green:0.74 blue:0.74 alpha:1.00];
    self.likeLabel.font = [UIFont systemFontOfSize:11];
    self.likeLabel.text = [NSString stringWithFormat:@"%ld", _model.praisenum];
    [self.view addSubview:_likeLabel];
    UIButton *button = (UIButton *)[self.view viewWithTag:1200];
    [self.likeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(button.right);
        make.bottom.equalTo(button.bottom);
        make.height.equalTo(button);
        make.width.equalTo(40);
    }];
    [self.likeLabel setTag:1201];
}

- (void)setUpHomeDetailTableView {
    XZHHomeTableView *tableView = [[XZHHomeTableView alloc] initWithFrame:self.view.frame];
    tableView.delegate = self;
    tableView.model = _model;
    
    [self.view addSubview:tableView];
}

- (void)shareHP:(XZHHomeModel *)model {
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@ [%@]",model.hp_content, model.hp_author]
                                     images:model.hp_img_url
                                        url:[NSURL URLWithString:@"http://www.jianshu.com/users/8763aac98cf7/latest_articles"]
                                      title:@"PIECE"
                                       type:SSDKContentTypeAuto];
    //调用自定义分享
    [JZShareCustom shareWithContent:shareParams Url:model.web_url  Title:nil Type:nil HomeModel:model MovieModel:nil];
}

- (void)setModel:(XZHHomeModel *)model {
    if (_model != model) {
        _model = model;
        
    
    }
}

- (void)changLikeImageView:(XZHHomeModel *)model {
    presentLikeCount = _model.praisenum;
    NSInteger likeCount =[[self.likeLabel text] integerValue];
    if(isLike == NO){
        [(UIButton *)[self.view viewWithTag:1200] setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateNormal];
        presentLikeCount = likeCount + 1;
        self.likeLabel.text = [NSString stringWithFormat:@"%ld", presentLikeCount];
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"like_%@",self.model.hpcontent_id]];
        
        isLike = YES;
    } else {
        isLike = NO;
        [(UIButton *)[self.view viewWithTag:1200] setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
        presentLikeCount = likeCount - 1;
        self.likeLabel.text = [NSString stringWithFormat:@"%ld", presentLikeCount];
    }
}

- (void)editNote:(XZHHomeModel *)model{
    XZHHomeNoteViewController *noteVC = [[XZHHomeNoteViewController alloc] init];
    noteVC.model = model;
    
    [self.navigationController pushViewController:noteVC animated:YES];
}

- (void)swipeTableView {
    XZHSearchViewController *searchVC = [[XZHSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
